﻿using EntityModel.PageListInfor;
using EntityModel.Procurement;
using IServices.ICurrency;
using IServices.IStock;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AikeApplets.Areas.Stock.Controllers
{
    [Area("Stock")]
    public class StockController : Controller
    {
        private readonly IStock _stock;
        private readonly ICurrencyServer _currencyServer;
        public StockController(IConfiguration configuration, IStock stock, ICurrencyServer currencyServer)
        {
            _stock = stock ?? throw new ArgumentNullException(nameof(stock));
            _currencyServer = currencyServer ?? throw new ArgumentNullException(nameof(currencyServer));
        }

        #region 视图模块
        public IActionResult Index()
        {
            return View();
        }
        public IActionResult AddStock()
        {
            return View();
        }

        [HttpGet]
        public async Task<IActionResult> UpdateStock(string stockID)
        {
            if (stockID == null) throw new ArgumentNullException(nameof(stockID));

            AikeApplet_Stocks result = await _stock.GetEntityStockAsync(stockID);

            return View(result);
        }
        #endregion


        #region 功能模块
        /// <summary>
        /// 显示采购单数据
        /// </summary>
        /// <param name="page">当前页</param>
        /// <param name="limit">显示条数</param>
        /// <returns></returns>
        public IActionResult GetData(int page, int limit, AikeApplet_Stocks applet_Stocks)
        {
            applet_Stocks.PageIndex = page;
            applet_Stocks.PageSize = limit;
            applet_Stocks.SortBy = "stockdate";
            applet_Stocks.SortOrder = SortAction.Desc;
            DbQueryResult result = _stock.GetPageListStockAsync(applet_Stocks).Result;
            var data = result.Data.AsEnumerable().Select(s => new
            {
                StockID = s.Field<string>("StockID"),
                PPID = s.Field<string>("PPID"),
                StockDate = s.Field<DateTime>("StockDate"),
                StockInDate = s.Field<DateTime>("StockInDate"),
                StockUser = s.Field<int>("StockUser"),  //创建人
                StockState = s.Field<int>("StockState") == 1 ? "未审核" : "已审核",
                StockDesc = s.Field<string>("StockDesc")
            }).ToList();
            return Ok(new { code = 0, count = result.TotalRecords, data = data });
        }

        /// <summary>
        /// 添加采购单
        /// </summary>
        /// <param name="applet_Stocks">采购单数据集</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> AddStockInfo(AikeApplet_Stocks applet_Stocks)
        {
            if (applet_Stocks == null) return _currencyServer.Fail("请添加正确格式的数据");

            await _stock.AddStocksAsync(applet_Stocks);

            return _currencyServer.Success("数据添加成功");
        }

        /// <summary>
        /// 修改采购单
        /// </summary>
        /// <param name="applet_Stocks">采购单数据集</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> UpdateStockInfo(AikeApplet_Stocks applet_Stocks)
        {
            if (applet_Stocks == null) throw new ArgumentNullException(nameof(applet_Stocks));

            await _stock.UpdateStocksAsync(applet_Stocks);

            return _currencyServer.Success("数据修改成功");
        }

        /// <summary>
        /// 删除采购单
        /// </summary>
        /// <param name="stockID">采购单编号</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> RemoveStockInfor(string stockID)
        {
            if (stockID == null) return _currencyServer.Fail("未获取到编号，请联系管理员");

            var data = await _stock.GetEntityStockAsync(stockID);

            if (data == null) return _currencyServer.Fail("数据删除失败，请联系管理员");

            if (!await _stock.DeletedStocksAsync(data))
            {
                return _currencyServer.Success("数据删除失败");
            }

            return _currencyServer.Success("数据删除成功");
        }
        #endregion
    }
}
